﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Drawing;

/// <summary>
/// Summary description for HtmlCapture
/// </summary>
public class HtmlCapture
{
    private WebBrowser web;
    private Timer tready;
    private Rectangle screen;
    private Size? imgsize = null;

    //an event that triggers when the html document is captured
    public delegate void HtmlCaptureEvent(object sender,
                         Uri url, Bitmap image);
    public event HtmlCaptureEvent HtmlImageCapture;

    //class constructor
    public HtmlCapture()
    {
        //initialise the webbrowser and the timer
        web = new WebBrowser();
        tready = new Timer();
        tready.Interval = 2000;
        screen = Screen.PrimaryScreen.Bounds;
        //set the webbrowser width and hight
        web.Width = screen.Width;
        web.Height = screen.Height;
        //suppress script errors and hide scroll bars
        web.ScriptErrorsSuppressed = true;
        web.ScrollBarsEnabled = false;
        //attached events
        web.Navigating +=
          new WebBrowserNavigatingEventHandler(web_Navigating);
        web.DocumentCompleted += new
          WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
        tready.Tick += new EventHandler(tready_Tick);
    }

    #region Public methods
    public void Create(string url)
    {
        imgsize = null;
        web.Navigate(url);
    }

    public void Create(string url, Size imgsz)
    {
        this.imgsize = imgsz;
        web.Navigate(url);
    }
    #endregion

    #region Events
    void web_DocumentCompleted(object sender,
             WebBrowserDocumentCompletedEventArgs e)
    {
        //start the timer
        tready.Start();
    }

    void web_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        //stop the timer   
        tready.Stop();
    }

    void tready_Tick(object sender, EventArgs e)
    {
        //stop the timer
        tready.Stop();

        //capture html as an image
        //...
    }
    #endregion

    
}